//
//  WCFindResult.m
//  WabbitCode
//
//  Created by William Towe on 3/10/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCFindResult.h"
#import "WCTextFile.h"


@implementation WCFindResult
- (void)dealloc {
	[_resultMessage release];
	[_resultAttributedString release];
	[super dealloc];
}

- (BOOL)isLeaf {
	switch ([self resultType]) {
		case WCFRTypeFile:
			return NO;
		case WCFRTypeMessage:
			return NO;
		case WCFRTypeMatch:
			return YES;
		default:
			return [super isLeaf];
	}
}

- (NSString *)name {
	switch ([self resultType]) {
		case WCFRTypeFile:
			return [NSString stringWithFormat:@"%@ - %@", [[self textFile] name], [[self textFile] absoluteFilePath]];
		case WCFRTypeMatch:
		case WCFRTypeMessage:
			return [self resultMessage];
		default:
			return nil;
	}
}

- (NSImage *)icon {
	switch ([self resultType]) {
		case WCFRTypeFile:
			return [[self textFile] fileIcon];
		case WCFRTypeMessage:
			return [NSImage imageNamed:@"FindResults16x16"];
		case WCFRTypeMatch:
		default:
			return nil;
	}
}

@synthesize resultType=_resultType,textFile=_textFile,resultRange=_resultRange,resultMessage=_resultMessage,resultAttributedString=_resultAttributedString;

@dynamic badgeCount;
- (NSUInteger)badgeCount {
	switch ([self resultType]) {
		case WCFRTypeFile:
			return [[self childNodes] count];
		case WCFRTypeMatch:
			return 0;
		case WCFRTypeMessage: {
			NSUInteger total = 0;
			
			for (WCFindResult *fr in [self childNodes]) {
				total += [fr badgeCount];
			}
			return total;
		}
		default:
			return 0;
	}
}

- (id)initWithType:(WCFRType)type textFile:(WCTextFile *)textFile range:(NSRange)range message:(NSString *)message; {
	if (!(self = [super init]))
		return nil;
	
	_resultType = type;
	_textFile = textFile;
	_resultRange = range;
	_resultMessage = [message retain];
	
	return self;
}

+ (id)findResultWithType:(WCFRType)type textFile:(WCTextFile *)textFile range:(NSRange)range message:(NSString *)message; {
	return [[[[self class] alloc] initWithType:type textFile:textFile range:range message:message] autorelease];
}
@end
